package jnpf.onlinedev.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.service.VisualdevService;
import jnpf.database.entity.DbLinkEntity;
import jnpf.base.service.DblinkService;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.exception.WorkFlowException;
import jnpf.model.visiual.*;
import jnpf.onlinedev.model.*;
import jnpf.onlinedev.model.OnlineDevListModel.OnlineDevListDataVO;
import jnpf.onlinedev.model.OnlineDevListModel.OnlineFieldsModel;
import jnpf.onlinedev.model.OnlineDevListModel.VisualColumnSearchVO;
import jnpf.onlinedev.service.VisualDevListService;
import jnpf.onlinedev.util.*;
import jnpf.onlinedev.util.onlineDevUtil.OnlineDevListUtils;
import jnpf.onlinedev.util.onlineDevUtil.OnlinePublicUtils;
import jnpf.util.JsonUtilEx;
import jnpf.base.util.VisualUtils;
import jnpf.base.entity.VisualdevEntity;
import jnpf.onlinedev.entity.VisualdevModelDataEntity;
import jnpf.exception.DataException;
import jnpf.base.model.template6.ColumnListField;
import jnpf.model.visiual.fields.FieLdsModel;
import jnpf.onlinedev.mapper.VisualdevModelDataMapper;
import jnpf.onlinedev.service.VisualdevModelDataService;
import jnpf.util.*;
import jnpf.util.JsonUtil;
import lombok.Cleanup;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @date 2021/3/16
 */
@Service
public class VisualdevModelDataServiceImpl extends ServiceImpl<VisualdevModelDataMapper, VisualdevModelDataEntity> implements VisualdevModelDataService {

    @Autowired
    private UserProvider userProvider;
    @Autowired
    private DblinkService dblinkService;
    @Autowired
    private VisualdevService visualdevService;
    @Autowired
    private VisualDevListService visualDevListService;


    @Override
    public List<VisualdevModelDataEntity> getList(String modelId) {
        QueryWrapper<VisualdevModelDataEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(VisualdevModelDataEntity::getVisualDevId, modelId);
        return this.list(queryWrapper);
    }

    @Override
    public List<FormDataField> fieldList(String id) {
        VisualdevEntity entity = visualdevService.getInfo(id);
        FormDataModel formData = JsonUtil.getJsonToBean(entity.getFormData(), FormDataModel.class);
        List<FieLdsModel> fieLdsModelList = JsonUtil.getJsonToList(formData.getFields(), FieLdsModel.class);
        List<FormDataField> formDataFieldList = new ArrayList<>();
        for (FieLdsModel fieLdsModel : fieLdsModelList) {
            if (!"".equals(fieLdsModel.getVModel()) && !JnpfKeyConsts.CHILD_TABLE.equals(fieLdsModel.getConfig().getJnpfKey()) && !"relationForm".equals(fieLdsModel.getConfig().getJnpfKey())) {
                FormDataField formDataField = new FormDataField();
                formDataField.setLabel(fieLdsModel.getConfig().getLabel());
                formDataField.setVModel(fieLdsModel.getVModel());
                formDataFieldList.add(formDataField);
            }
        }
        return formDataFieldList;
    }

    @Override
    public List<Map<String, Object>> getPageList(VisualdevEntity entity, PaginationModel paginationModel) {
        String json = null;
        if (StringUtil.isNotEmpty(paginationModel.getKeyword())) {
            Map<String, Object> map = new HashMap<>();
            map.put(paginationModel.getRelationField(), paginationModel.getKeyword());
            json = JsonUtil.getObjectToString(map);
        }
        paginationModel.setJson(json);
        List<Map<String, Object>> dataList = visualDevListService.getDataList(entity, paginationModel);
        return dataList;
    }

    @Override
    public List<Map<String, Object>> exportData(String[] keys, PaginationModelExport paginationModelExport, VisualdevEntity visualdevEntity) {
        PaginationModel paginationModel =new PaginationModel();
        BeanUtil.copyProperties(paginationModelExport,paginationModel);

        List<OnlineDevListDataVO> noSwapDataList;
        ColumnDataModel columnDataModel = JsonUtil.getJsonToBean(visualdevEntity.getColumnData(),ColumnDataModel.class);
        List<VisualColumnSearchVO> searchVOList = new ArrayList<>();
        //封装查询条件
        if (StringUtil.isNotEmpty(paginationModel.getJson())){
            Map<String, Object> keyJsonMap = JsonUtil.stringToMap(paginationModel.getJson());
            searchVOList= JsonUtil.getJsonToList(columnDataModel.getSearchList(),VisualColumnSearchVO.class);
            searchVOList =	searchVOList.stream().map(searchVO->{
                searchVO.setValue(keyJsonMap.get(searchVO.getVModel()));
                return searchVO;
            }).filter(vo->vo.getValue()!=null).collect(Collectors.toList());
            //左侧树查询
            boolean b =false;
            if (columnDataModel.getTreeRelation()!=null){
                b = keyJsonMap.keySet().stream().anyMatch(t -> t.equalsIgnoreCase(String.valueOf(columnDataModel.getTreeRelation())));
            }
            if (b && keyJsonMap.size()>searchVOList.size()){
                String relation =String.valueOf(columnDataModel.getTreeRelation());
                VisualColumnSearchVO vo =new VisualColumnSearchVO();
                vo.setSearchType("1");
                vo.setVModel(relation);
                vo.setValue(keyJsonMap.get(relation));
                searchVOList.add(vo);
            }
        }
        //判断有无表
        if (OnlinePublicUtils.isUseTables(visualdevEntity.getTables())){
            //当前用户信息
            UserInfo userInfo = userProvider.get();
            //菜单id
            String moduleId = paginationModel.getMenuId();
            String reg = "^[jnpf_]\\S*_jnpf\\S*";
            searchVOList.stream().filter(c-> c.getVModel().matches(reg)).forEach(
                cl->{
                    String s = cl.getVModel();
                    String s1 = s.substring(s.lastIndexOf("jnpf_")).replace("jnpf_","");
                    cl.setVModel(s1);
                }
            );
            noSwapDataList =visualDevListService.getListWithTable(visualdevEntity,paginationModel,userInfo,moduleId,searchVOList);
        }else{
            noSwapDataList =visualDevListService.getWithoutTableData(visualdevEntity.getId());
            noSwapDataList = visualDevListService.getList(noSwapDataList, searchVOList, paginationModel);
        }

        //数据转换
        FormDataModel formDataModel = JsonUtil.getJsonToBean(visualdevEntity.getFormData(), FormDataModel.class);
        List<OnlineFieldsModel> swapDataVoList = JsonUtil.getJsonToList(formDataModel.getFields(),OnlineFieldsModel.class);
        //递归处理控件
        List<OnlineFieldsModel> allFormDataModelList = new ArrayList<>();
        OnlinePublicUtils.recursionFields(swapDataVoList,allFormDataModelList);
        allFormDataModelList=allFormDataModelList.stream().filter(model->model.getConfig().getJnpfKey()!=null && StringUtil.isNotEmpty(model.getVModel())).collect(Collectors.toList());
        noSwapDataList = OnlineDevListUtils.getSwapList(noSwapDataList, allFormDataModelList,visualdevEntity.getId());

        List<Map<String, Object>> realList = noSwapDataList.stream().map(t->t.getData()).collect(Collectors.toList());
        realList= realList.stream().map(rl-> {
            Map<String,Object> map = new HashMap<>();
            for (String key : keys){
                map.put(key,rl.get(key));
            }
            return map;
        }).collect(Collectors.toList());
        return realList;
    }


    @Override
    public VisualdevModelDataEntity getInfo(String id) {
        QueryWrapper<VisualdevModelDataEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(VisualdevModelDataEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public VisualdevModelDataInfoVO infoDataChange(String id, VisualdevEntity visualdevEntity) throws IOException, ParseException, DataException, SQLException {
        Map<String, Object> formData = JsonUtil.stringToMap(visualdevEntity.getFormData());
        List<FieLdsModel> modelList = JsonUtil.getJsonToList(formData.get("fields").toString(), FieLdsModel.class);
        //去除模板多级控件
        modelList = VisualUtils.deleteMore(modelList);

        QueryWrapper<VisualdevModelDataEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(VisualdevModelDataEntity::getId, id);
        VisualdevModelDataEntity visualdevModelDataEntity = this.getOne(queryWrapper);
        if (visualdevModelDataEntity != null) {
            Map<String, Object> newDataMap = JsonUtil.stringToMap(visualdevModelDataEntity.getData());
            Map<String, Object> newDataMapOperate = new HashMap<>(16);
            newDataMapOperate.putAll(newDataMap);
            Map<String, Object> newMainDataMap = new HashMap<>(16);
            for (Map.Entry<String, Object> entryMap : newDataMapOperate.entrySet()) {
                String key = entryMap.getKey();
                if (!key.contains("table")) {
                    newMainDataMap.put(key, entryMap.getValue());
                    newDataMap.remove(key);
                }
            }
            //添加主表数据
            newDataMap.put("main", JSON.toJSONString(newMainDataMap));
            int t = 0;
            //转换主表和子表数据
            for (FieLdsModel fieLdsModel : modelList) {
                for (Map.Entry<String, Object> entryMap : newDataMap.entrySet()) {
                    //转换为真实数据
                    if (entryMap.getKey().equals(fieLdsModel.getVModel()) || "main".equals(entryMap.getKey())) {
                        List<VisualdevModelDataEntity> list = new ArrayList<>();
                        //将查询的关键字json转成map
                        Map<String, Object> keyJsonMap = new HashMap<>(16);
                        List<FieLdsModel> formDatas;
                        if ("main".equals(entryMap.getKey()) && t == 0) {
                            formDatas = modelList;
                            VisualdevModelDataEntity entity = new VisualdevModelDataEntity();
                            entity.setData(entryMap.getValue().toString());
                            list.add(entity);
                            t++;
                            //将关键字查询传输的id转换成名称
                            Map<String, Object> keyAndList = KeyDataUtil.getKeyData(formDatas, keyJsonMap, list, visualdevEntity.getId(), 1);

                            //替换静态数据模板选项值
                            keyAndList = ListDataHandler.getRealData(formDatas, keyJsonMap, JsonUtil.getJsonToList(keyAndList.get(DataTypeConst.LIST), VisualdevModelDataEntity.class));
                            //系统自动生成字段转换
                            list = VisualUtils.stringToList(formDatas, JsonUtil.getJsonToList(keyAndList.get(DataTypeConst.LIST), VisualdevModelDataEntity.class));
                            //字符串转数组
                            list = AutoFeildsUtil.autoFeildsList(formDatas, list);
                            list = TableInfoUtil.NoTableInfo(modelList, list, visualdevModelDataEntity.getData());
                            entryMap.setValue(list);
                        } else if (entryMap.getKey().equals(fieLdsModel.getVModel())) {
                            formDatas = JsonUtil.getJsonToList(fieLdsModel.getConfig().getChildren(), FieLdsModel.class);
                            List<Map<String, Object>> childMapList = (List<Map<String, Object>>) entryMap.getValue();
                            for (Map<String, Object> objectMap : childMapList) {
                                VisualdevModelDataEntity entity = new VisualdevModelDataEntity();
                                entity.setData(JsonUtilEx.getObjectToString(objectMap));
                                list.add(entity);
                            }
                            //将关键字查询传输的id转换成名称
                            Map<String, Object> keyAndList = KeyDataUtil.getKeyData(formDatas, keyJsonMap, list, visualdevEntity.getId(), 0);

                            //替换静态数据模板选项值
                            keyAndList = ListDataHandler.getRealData(formDatas, keyJsonMap, JsonUtil.getJsonToList(keyAndList.get(DataTypeConst.LIST), VisualdevModelDataEntity.class));
                            //系统自动生成字段转换
                            list = VisualUtils.stringToList(formDatas, JsonUtil.getJsonToList(keyAndList.get(DataTypeConst.LIST), VisualdevModelDataEntity.class));
                            //字符串转数组
                            list = AutoFeildsUtil.autoFeildsList(formDatas, list);
                            entryMap.setValue(list);
                        }
                    }
                }
            }

            List<VisualdevModelDataEntity> mainList = (List<VisualdevModelDataEntity>) newDataMap.get("main");

            Map<String, Object> realDataMap = JsonUtil.stringToMap(mainList.get(0).getData());
            newDataMap.remove("main");

            for (Map.Entry<String, Object> entryMap : newDataMap.entrySet()) {
                List<VisualdevModelDataEntity> childList = (List<VisualdevModelDataEntity>) entryMap.getValue();
                List<Map<String, Object>> array = new ArrayList<>();
                for (VisualdevModelDataEntity childEntity : childList) {
                    array.add(JsonUtil.stringToMap(childEntity.getData()));
                }
                entryMap.setValue(array);
            }
            realDataMap.putAll(newDataMap);
            String tmp1 = JsonUtilEx.getObjectToString(realDataMap);
            VisualdevModelDataInfoVO vo = new VisualdevModelDataInfoVO();
            vo.setData(tmp1);
            vo.setId(visualdevModelDataEntity.getId());
            return vo;
        }
        return null;
    }

    /**
     * 获取单条信息时，主表将时间字符串转时间戳，子表将字符串类型的数组时间戳转成Json数组时间戳
     *
     * @param id
     * @param visualdevEntity
     * @return
     * @throws DataException
     * @throws ParseException
     */
    @Override
    public VisualdevModelDataInfoVO tableInfo(String id, VisualdevEntity visualdevEntity) throws DataException, SQLException, ParseException, IOException {
        Map<String, Object> formData = JsonUtil.stringToMap(visualdevEntity.getFormData());
        List<FieLdsModel> modelList = JsonUtil.getJsonToList(formData.get("fields").toString(), FieLdsModel.class);

        //去除模板多级控件
        modelList = VisualUtils.deleteMore(modelList);

        List<Map<String, Object>> tableMapList = JsonUtil.getJsonToListMap(visualdevEntity.getTables());
        String mainTable = VisualUtils.getMainTable(tableMapList).get("table").toString();
        DbLinkEntity linkEntity = dblinkService.getInfo(visualdevEntity.getDbLinkId());
        @Cleanup Connection conn = VisualUtils.getDataConn(linkEntity);
        //获取主键
        String pKeyName = VisualUtils.getpKey(conn, mainTable);

        StringBuilder mainfeild = new StringBuilder();
        String main = "select * from" + " " + tableMapList.get(0).get("table") + " where " + pKeyName + "='" + id + "'";
        List<Map<String, Object>> mainMap = VisualUtils.getTableDataInfo(main, linkEntity);
        return TableInfoUtil.getTableInfo(pKeyName, mainfeild, modelList, mainMap, tableMapList, linkEntity);
    }


    /**
     * 获取单条信息时，主表将时间字符串转时间戳，子表将字符串类型的数组时间戳转成Json数组时间戳(真实数据转换)
     *
     * @param id
     * @param visualdevEntity
     * @return
     * @throws DataException
     * @throws ParseException
     */
    @Override
    public VisualdevModelDataInfoVO tableInfoDataChange(String id, VisualdevEntity visualdevEntity) throws DataException, IOException, SQLException, ParseException {
        Map<String, Object> formData = JsonUtil.stringToMap(visualdevEntity.getFormData());

        List<FieLdsModel> modelList = JsonUtil.getJsonToList(formData.get("fields").toString(), FieLdsModel.class);
        //去除模板多级控件
        modelList = VisualUtils.deleteMore(modelList);
        List<Map<String, Object>> tableMapList = JsonUtil.getJsonToListMap(visualdevEntity.getTables());
        String mainTable = tableMapList.get(0).get("table").toString();
        DbLinkEntity linkEntity = dblinkService.getInfo(visualdevEntity.getDbLinkId());
        return TableInfoUtil.getTableInfoDataChange(id, modelList, tableMapList, mainTable, linkEntity, "");
    }

    @Override
    public ActionResult visualCreate(VisualdevEntity visualdevEntity, Map<String, Object> dataMap, VisualdevModelDataCrForm visualdevModelDataCrForm, String mainId) throws WorkFlowException {
        if (!StringUtil.isEmpty(visualdevEntity.getTables()) && !OnlineDevData.TABLE_CONST.equals(visualdevEntity.getTables())) {
            if (visualdevEntity.getWebType() != null && visualdevEntity.getWebType().equals("3")) {
                //流程提交
                if (visualdevModelDataCrForm.getStatus().equals(FlowStatusEnum.submit.getMessage())) {
                    VisualUtils.submitFlowTask(visualdevEntity, mainId, dataMap, userProvider.get());
                    return ActionResult.success("提交成功，请耐心等待");
                } else {
                    return ActionResult.success("保存成功");
                }
            } else {
                return ActionResult.success("新建成功");
            }
        } else {
            VisualdevModelDataEntity entity = new VisualdevModelDataEntity();
            entity.setData(JsonUtilEx.getObjectToString(dataMap));
            entity.setVisualDevId(visualdevEntity.getId());
            entity.setId(mainId);
            entity.setSortcode(RandomUtil.parses());
            entity.setCreatortime(new Date());
            entity.setCreatoruserid(userProvider.get().getUserId());
            entity.setEnabledmark(1);
            this.save(entity);
            if (visualdevEntity.getWebType() != null && visualdevEntity.getWebType().equals("3")) {
                //流程提交
                if (visualdevModelDataCrForm.getStatus().equals(FlowStatusEnum.submit.getMessage())) {
                    VisualUtils.submitFlowTask(visualdevEntity, mainId, dataMap, userProvider.get());
                    return ActionResult.success("提交成功，请耐心等待");
                } else {
                    return ActionResult.success("保存成功");
                }
            } else {
                return ActionResult.success("新建成功");
            }
        }
    }

    @Override
    public ActionResult visualUpdate(String id, VisualdevEntity visualdevEntity, Map<String, Object> map, VisualdevModelDataUpForm visualdevModelDataUpForm) throws WorkFlowException {
        if (StringUtil.isEmpty(visualdevEntity.getTables()) || OnlineDevData.TABLE_CONST.equals(visualdevEntity.getTables())) {
            VisualdevModelDataEntity entity = new VisualdevModelDataEntity();
            entity.setData(JsonUtilEx.getObjectToString(map));
            entity.setVisualDevId(visualdevEntity.getId());
            entity.setId(id);
            entity.setLastModifyTime(new Date());
            entity.setLastmodifyuserid(userProvider.get().getUserId());
            this.updateById(entity);
        }
        if (visualdevEntity.getWebType() != null && visualdevEntity.getWebType().equals("3")) {
            //流程提交
            if (visualdevModelDataUpForm.getStatus().equals(FlowStatusEnum.submit.getMessage())) {
                VisualUtils.submitFlowTask(visualdevEntity, id, map, userProvider.get());
                return ActionResult.success("提交成功，请耐心等待");
            } else {
                return ActionResult.success("保存成功");
            }
        } else {
            return ActionResult.success("更新成功");
        }
    }


    /**
     * 主表添加时将时间转换成字符串时间，子表不做改变
     *
     * @param visualdevEntity
     * @param visualdevModelDataCrForm
     * @throws DataException
     */
    @SneakyThrows
    @Override
    public ActionResult create(VisualdevEntity visualdevEntity, VisualdevModelDataCrForm visualdevModelDataCrForm) throws DataException, SQLException {
        Map<String, Object> allDataMap = JsonUtil.stringToMap(visualdevModelDataCrForm.getData());
        List<FieLdsModel> fieLdsModelList = JsonUtil.getJsonToList(JsonUtil.stringToMap(visualdevEntity.getFormData()).get("fields"), FieLdsModel.class);
        //去除模板多级控件
        fieLdsModelList = VisualUtils.deleteMore(fieLdsModelList);

        //去除无意义控件
        fieLdsModelList = VisualUtils.deleteVmodel(fieLdsModelList);

        //生成系统自动生成字段
        allDataMap = AutoFeildsUtil.createFeilds(fieLdsModelList, allDataMap);

        Map<String, Object> newMainDataMap = JsonUtil.stringToMap(visualdevModelDataCrForm.getData());
        //生成id
        String mainId = RandomUtil.uuId();
        if (!StringUtil.isEmpty(visualdevEntity.getTables()) && !OnlineDevData.TABLE_CONST.equals(visualdevEntity.getTables())) {
            //没有选择数据源,默认配置文件的数据源
            DbLinkEntity linkEntity = dblinkService.getInfo(visualdevEntity.getDbLinkId());
            OnlineDevDbUtil.insertTable(visualdevEntity, fieLdsModelList, allDataMap, newMainDataMap, linkEntity, mainId);
            if (visualdevEntity.getWebType() != null && visualdevEntity.getWebType().equals("3")) {
                //流程提交
                if (visualdevModelDataCrForm.getStatus().equals(FlowStatusEnum.submit.getMessage())) {
                    VisualUtils.submitFlowTask(visualdevEntity, mainId, newMainDataMap, userProvider.get());
                    return ActionResult.success("提交成功，请耐心等待");
                } else {
                    return ActionResult.success("保存成功");
                }
            } else {
                return ActionResult.success("新建成功");
            }
        } else {
            VisualdevModelDataEntity entity = new VisualdevModelDataEntity();
            entity.setData(JsonUtilEx.getObjectToString(allDataMap));
            entity.setVisualDevId(visualdevEntity.getId());
            entity.setId(mainId);
            entity.setSortcode(RandomUtil.parses());
            entity.setCreatortime(new Date());
            entity.setCreatoruserid(userProvider.get().getUserId());
            entity.setEnabledmark(1);
            this.save(entity);
            if (visualdevEntity.getWebType() != null && visualdevEntity.getWebType().equals("3")) {
                //流程提交
                if (visualdevModelDataCrForm.getStatus().equals(FlowStatusEnum.submit.getMessage())) {
                    VisualUtils.submitFlowTask(visualdevEntity, mainId, newMainDataMap, userProvider.get());
                    return ActionResult.success("提交成功，请耐心等待");
                } else {
                    return ActionResult.success("保存成功");
                }
            } else {
                return ActionResult.success("新建成功");
            }
        }

    }

    @SneakyThrows
    @Override
    public ActionResult update(String id, VisualdevEntity visualdevEntity, VisualdevModelDataUpForm visualdevModelDataUpForm) throws DataException, SQLException {

        Map<String, Object> allDataMap = JsonUtil.stringToMap(visualdevModelDataUpForm.getData());
        List<FieLdsModel> fieLdsModelList = JsonUtil.getJsonToList(JsonUtil.stringToMap(visualdevEntity.getFormData()).get("fields"), FieLdsModel.class);
        //去除模板多级控件
        fieLdsModelList = VisualUtils.deleteMore(fieLdsModelList);
        //去除无意义控件
        fieLdsModelList = VisualUtils.deleteVmodel(fieLdsModelList);

        //生成系统自动生成字段
        allDataMap = AutoFeildsUtil.updateFeilds(fieLdsModelList, allDataMap);

        Map<String, Object> newMainDataMap = JsonUtil.stringToMap(visualdevModelDataUpForm.getData());

        boolean flag;
        if (!StringUtil.isEmpty(visualdevEntity.getTables()) && !OnlineDevData.TABLE_CONST.equals(visualdevEntity.getTables())) {
            DbLinkEntity linkEntity = dblinkService.getInfo(visualdevEntity.getDbLinkId());
            Map<String, Object> dataMap = OnlineDevDbUtil.swapTimeType(allDataMap, fieLdsModelList);
            flag = OnlineDevDbUtil.updateTables(id, dataMap, fieLdsModelList, linkEntity, visualdevEntity);
            if (!flag) {
                return ActionResult.fail("更新失败，数据不存在");
            }
        }
        VisualdevModelDataEntity entity = new VisualdevModelDataEntity();
        entity.setData(JsonUtilEx.getObjectToString(allDataMap));
        entity.setVisualDevId(visualdevEntity.getId());
        entity.setId(id);
        entity.setLastModifyTime(new Date());
        entity.setLastmodifyuserid(userProvider.get().getUserId());
        this.updateById(entity);
        if (visualdevEntity.getWebType() != null && visualdevEntity.getWebType().equals("3")) {
            //流程提交
            if (visualdevModelDataUpForm.getStatus().equals(FlowStatusEnum.submit.getMessage())) {
                VisualUtils.submitFlowTask(visualdevEntity, id, newMainDataMap, userProvider.get());
                return ActionResult.success("提交成功，请耐心等待");
            } else {
                return ActionResult.success("保存成功");
            }
        } else {
            return ActionResult.success("更新成功");
        }
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(VisualdevModelDataEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }

    @Override
    public boolean tableDelete(String id, VisualdevEntity visualdevEntity) throws DataException, SQLException {
        DbLinkEntity linkEntity = dblinkService.getInfo(visualdevEntity.getDbLinkId());
        return OnlineDevDbUtil.deleteTable(id, visualdevEntity, linkEntity);
    }

    @Override
    public ActionResult tableDeleteMore(List<String> ids, VisualdevEntity visualdevEntity) throws DataException, SQLException {
        DbLinkEntity linkEntity = dblinkService.getInfo(visualdevEntity.getDbLinkId());
        return OnlineDevDbUtil.deleteTables(ids, visualdevEntity, linkEntity);
    }


}
